package model;

import java.util.Collection;
import java.util.Vector;

public class Automaton {

	final private State startState;
	final private StateSet states;
	final private Collection<Transition> transitions;
	
	
	public Automaton(){
		this.states = StateSet.create();
		this.startState = new State("start");
		this.states.add(startState);
		this.transitions = new Vector<Transition>();
	}
	
	public Result recognizes(final String input){
		final Configuration configuration = new Configuration(this, startState, input, 0);
		final StepperManager manager = new StepperManager(this, configuration);
		
		
		return manager.getResult();
	}
	
	
	public void createTransition(final char input,final char output, final State origin, final State target){
		if(this.states.contains(target) && this.states.contains(origin))
			new Transition(input, output, origin, target);
		else
			throw new Error("invalid Transition");
	}

	public State getStartState() {
		return this.startState;
	}
	
	public StateSet getStates() {
		return states;
	}

	public Collection<Transition> getTransitions() {
		return transitions;
	}

	public void put(final State state) {
		this.getStates().add(state);
	}
	

	public Collection<Transition> getOutgoingTransitions(final State state) {
		final Vector<Transition> result = new Vector<Transition>();
		for(final Transition t : this.transitions){
			if(t.equals(state)){
				result.add(t);
			}
		}
		
		return result;
	}

	
	public Collection<Transition> getSuitableTransitions(final State state, final char input){
		final Vector<Transition> result = new Vector<Transition>();
		for(final Transition t : this.getOutgoingTransitions(state)){
			if (t.input == input){
				result.add(t);
			}
		}
		
		return result;
		
	}
}
